package com.my_musc_service.common.timedtask.scheduler;

import com.my_musc_service.common.timedtask.job.StatisticCommentJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @author Zhu Dunfeng
 * @date 2021/8/29 19:49
 */
@Component
public class StatisticsScheduler {

//    @PostConstruct
    public void statistics() throws InterruptedException, SchedulerException {
        //1、创建调度器Scheduler
        SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        Scheduler scheduler = schedulerFactory.getScheduler();

        //2、创建JobDetail实例，并与TestJob类绑定（Job执行内容）
        JobDetail jobDetail = JobBuilder.newJob(StatisticCommentJob.class)
                .withIdentity("job1", "group1")
                .build();

        //3、构建Trigger实例，项目启动一分钟后开始执行一次，每10分钟执行一次
        CronTrigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("trigger1", "traggerGroup1")
                .usingJobData("11", "11")
                .startNow()
                .withSchedule(CronScheduleBuilder.cronSchedule("1/5 * * * * ? *"))
                .build();


        //4、执行
        scheduler.scheduleJob(jobDetail, trigger);
        System.out.println("--------scheduler start ! ------------");
        scheduler.start();
    }



}
